package com.imooc.miaosha.redis;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Created by ice on 2018/2/7
 *
 * The class is responsible for connecting with Redis.
 */
@Service
public class RedisService {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 获取单个对象
     *
     * @param prefix    Key prefix
     * @param suffix    Key suffix.
     * @param clazz     获取的对象类型
     */
    public <T> T get(KeyPrefix prefix, String suffix, Class<T> clazz) {
        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();

            // The stored key.
            String key = prefix.getPrefix() + suffix;
            String data = jedis.get(key);
            T bean = stringToBean(data, clazz);
            return bean;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * Setting the value to Redis.
     */
    public <T> boolean set(KeyPrefix prefix, String suffix, T value) {
        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();
            String data = beanToString(value);

            if (data == null || data.length() <= 0) {
                return false;
            }

            String key = prefix.getPrefix() + suffix;
            int expire = prefix.expireSeconds();
            if (expire <= 0) {
                jedis.set(key, data);
            } else {
                jedis.setex(key, expire, data);
            }
            return true;
        } finally {
            returnToPool(jedis);
        }


    }

    /**
     * Bean to String.
     */
    private <T> String beanToString(T value) {
        if (value == null) {
            return null;
        }

        Class<?> clazz = value.getClass();

        if (clazz == int.class || clazz == Integer.class) {
            return "" + value;
        } else if (clazz == long.class || clazz == Long.class) {
            return "" + value;
        } else if (clazz == String.class) {
            return (String) value;
        } else {
            return JSON.toJSONString(value);
        }
    }


    /**
     * 返回连接到对象池
     * @param jedis
     */
    private void returnToPool(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    /**
     * Transferring String to bean.
     */
    @SuppressWarnings("unchecked")
    private <T> T stringToBean(String data, Class<T> clazz) {
        if (data == null || data.length() <= 0 || clazz == null) {
            return null;
        }

        if (clazz == int.class || clazz == Integer.class) {
            return (T) Integer.valueOf(data);
        } else if (clazz == long.class || clazz == Long.class) {
            return (T) Long.valueOf(data);
        } else if (clazz == String.class) {
            return (T) data;
        } else {
            return JSON.toJavaObject(JSON.parseObject(data), clazz);
        }
    }

    /**
     * Determine if Key exists.
     */
    public boolean exists(KeyPrefix prefix, String suffix) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.exists(prefix.getPrefix() + suffix);
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * Increase one.
     */
    public Long incr(KeyPrefix prefix, String suffix) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.incr(prefix.getPrefix() + suffix);
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * Decrease one.
     */
    public Long decr(KeyPrefix prefix, String suffix) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.decr(prefix.getPrefix() + suffix);
        } finally {
            returnToPool(jedis);
        }
    }

}
